'''
Created on Apr 1, 2012

@author: e911miri
'''
import mysql.connector
import config

config = config.dbConfig.dbinfo().copy()
conn = mysql.connector.Connect(**config)

def runQuery(query, parameters=(), single=False):
    c = conn.cursor()
    try:
        c.execute(query, parameters)
    except:
        raise
    if single:
        results = c.fetchone()
    else:
        results = c.fetchall()
    conn.commit()
    c.close()
    return results

def sanitize(reg_num=0, pin='0000'):
    #use this method to handle injection and keep hackers away
    if len(reg_num) < 11 and len(pin) == 4:
        return {'reg_num':reg_num, 'pin': pin}
    else: return False
    
def cachecombo(id):    
    combo = runQuery("SELECT a,b,c,d FROM combinations WHERE idcombination= %s", (id,), single=True)
    if combo:
        return combo
    else: return None




